/*
-----------------------------------------------------------------------------
Filename:    SpaceProject.cpp

-----------------------------------------------------------------------------
*/

#include "SpaceProject.h"

enum devices{
	NONE		= 0,
	KEYBOARD	= 1,
	IOTRACKER	= 2,
	JOYSTICK	= 3,
	GAMEPAD		= 4
};

#define ControlPlayer1 JOYSTICK
#define ControlPlayer2 GAMEPAD

//-------------------------------------------------------------------------------------
SpaceProject::SpaceProject(void)
{	
}
//-------------------------------------------------------------------------------------
SpaceProject::~SpaceProject(void)
{
}

//-------------------------------------------------------------------------------------
void SpaceProject::createScene(void)
{		
	// Setup CEGUI MENU
	menuCEGUI::createScene();
	// create sky, ground, lights ...
	setupScenario();
	// setup sound
	setupSound();
	// VRPN server conexion
	setupVRPN();
	// Collision System -- MOC 
	mCollisionTools = new MOC::CollisionTools(mSceneMgr);

	// PLAYERS
	Player1 = new Ship(mSceneMgr , mCollisionTools, mSoundMgr, 1);
	Player1->shipNode->setPosition(-20.0, -10.0, -1.0);
	Player2 = new Ship(mSceneMgr, mCollisionTools, mSoundMgr, 2);
	Player2->shipNode->setPosition(-5.0, -10.0, -1.0);
	Player2->shipNode->setVisible(false);
	
	// ENEMYS
	nEnemies = 5;
	Enemies = new Enemy*[nEnemies];
	for(int i=0; i<nEnemies; i++) Enemies[i] = new Enemy(mSceneMgr, mCollisionTools, i+1);
	// Enemies positions
	Enemies[0]->enemyNode->setPosition(34.9919 , -20.0, -186.883);
	Enemies[1]->enemyNode->setPosition(136.16  , -18.0, 141.965 );	
	Enemies[2]->enemyNode->setPosition(-5.0, 60.0, -111.516);
	Enemies[3]->enemyNode->setPosition(-220.635, 90.0 , 118.613 );
	Enemies[4]->enemyNode->setPosition(-10.3124, -20.0, 90.0659 );
	
	
	// ---------------------------------------------------	
	// camera speed
	mCameraMove = 30;	
	// ----------------------------------------------------

	// vars for VRPN
	if (ControlPlayer1 == IOTRACKER) targetNode = Player1->shipNode;
	if (ControlPlayer2 == IOTRACKER) targetNode = Player2->shipNode;

	// control with KEYBOARD, IOTRACKER, JOYSTICK, GAMEPAD

}
// -----------------------------------------------------------------------------
// Game Functions
// -----------------------------------------------------------------------------
void SpaceProject::setupScenario(void){

	// Skybox
	mSceneMgr->setSkyDome(true, "CloudySky", 5, 8);
	
	// Set ambient light
	mSceneMgr->setAmbientLight(Ogre::ColourValue(0.5, 0.5, 0.5));
	// shadows
	mSceneMgr->setShadowTechnique(Ogre::SHADOWTYPE_STENCIL_ADDITIVE);
	
	// Create a light
	Ogre::Light* l = mSceneMgr->createLight("MainLight");
	l->setPosition(0,150,30);
	
	// Niebla
	Ogre::ColourValue fadeColour(0.1, 0.09, 0.42);
	mSceneMgr->setFog(Ogre::FOG_LINEAR, fadeColour, 0.0, 500, 11000);

	// racetrack and terrain
	Ogre::Entity* racetrackEntity = mSceneMgr->createEntity("RaceTrack", "racetrack.mesh");
	racetrackEntity->setQueryFlags(RACETRACK_MASK);
	Ogre::SceneNode* racetrackNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
	racetrackNode->attachObject(racetrackEntity);
	racetrackNode->scale(8,8,8);
	

	// Goal Plane
	goalPlane.redefine(Ogre::Vector3::UNIT_Z, Ogre::Vector3(0,0,0) );
	Ogre::MeshManager::getSingleton().createPlane("goalPlane", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
        goalPlane, 90, 30, 1, 1, true, 1, 12, 12, Ogre::Vector3::UNIT_Y);

	Ogre::Entity* prueba = mSceneMgr->createEntity("goalEntity", "goalPlane");
	prueba->setQueryFlags(GOAL_MASK);
	Ogre::SceneNode* nodeGoal = mSceneMgr->getRootSceneNode()->createChildSceneNode();
	nodeGoal->attachObject(prueba);
	nodeGoal->translate(0,-20,0);
	nodeGoal->setVisible(false);
}
//-------------------------------------------------------------------------------------

void SpaceProject::setupVRPN(void){


	// vrpn iotracker
	iotracker = new vrpn_Tracker_Remote("iotracker@161.67.196.59:3883");
	iotracker->register_change_handler(this, handleIotracker);
				
	vrpn_TRACKERCB blank = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
	data = blank; 

	iotrackerButton = new vrpn_Button_Remote("Keyboard0@161.67.196.60");
	iotrackerButton->register_change_handler(this, handle_iotracker_Button);
	
	// Joystick
	joystickAnalog = new vrpn_Analog_Remote("joyWin32_1@161.67.196.60:3883");
	joystickAnalog->register_change_handler( this, handle_joystick_analog );
	joystickButton = new vrpn_Button_Remote("joyWin32_1@161.67.196.60:3883");
	joystickButton->register_change_handler(this, handle_joystick_Button);
	
	// Gamepad
	gamepadAnalog = new vrpn_Analog_Remote("joyWin32_2@161.67.196.60:3883");
	gamepadAnalog->register_change_handler( this, handle_gamepad_analog );
	gamepadButton = new vrpn_Button_Remote("joyWin32_2@161.67.196.60:3883");
	gamepadButton->register_change_handler(this, handle_gamepad_Button);	

}

// -----------------------------------------------------------------------------
// Sound
void SpaceProject::setupSound(void){
	
	mSoundMgr = SoundManager::createManager();
	mSoundMgr->init();
	mSoundMgr->setAudioPath( (char*) "..\\..\\media\\sounds\\" );

	// load audio
	mSoundMgr->loadAudio( "disparo.wav", &shootSound, false);
	mSoundMgr->loadAudio( "lanzallamas.wav", &flameSound, false);
	mSoundMgr->loadAudio( "acelerar.wav", &acelerateSound, true);
	mSoundMgr->loadAudio( "checkin.wav", &checkinSound, false);
	mSoundMgr->loadAudio( "zombie.wav", &zombieSound, false);

}
// -----------------------------------------------------------------------------
// On each Frame
bool SpaceProject::frameRenderingQueued(const Ogre::FrameEvent& evt)
{
	bool ret = menuCEGUI::frameRenderingQueued(evt);
	if (!ret) return false;
	
	// si comienzo
	if (mGAMEON){

		// KEYBOARD , IOTRACKER, GAMEPAD , JOYSTICK
		// iotracker
		if( (ControlPlayer1 == IOTRACKER)||(ControlPlayer2 == IOTRACKER) ){
			iotracker->mainloop();
			iotrackerButton->mainloop();
			if(!processIotrackerInput(evt)) return false;
		}		
		//jostick
		if( (ControlPlayer1 == JOYSTICK)||(ControlPlayer2 == JOYSTICK) ){
			joystickAnalog->mainloop();
			joystickButton->mainloop();
		}		
		//gamepad
		if( (ControlPlayer1 == GAMEPAD)||(ControlPlayer2 == GAMEPAD) ){
			gamepadAnalog->mainloop();
			gamepadButton->mainloop();
		}
	
		// -------------------------------------------------------
		// Players Movement --- if 3 sec of START
		if (mSTART){
			Player1->moveShip(evt);						// Player 1

			// Player 2
			if(multiplayerOn) {
				Player2->moveShip(evt);
				Player2->shipNode->setVisible(true,true);
				//#define ControlPlayer2 KEYBOARD
			}else{
				Player2->shipNode->setVisible(false,true);
				//#define ControlPlayer2 NONE
			}
		}
			// -------------------------------------------------------
	// Enemy Movement
		for(int i=0; i<nEnemies; i++) Enemies[i]->moveEnemy(evt);

		// hit enemies
		hitEnemies();
		// count laps
		countLaps();
		// VICTORY
		checkVictory();
		// Count speed
		countSpeed();

	} // fin mGAMEON


	// Final return
	return true;
}

// ---------------------------------------------------------------------------
// Check who wins
void SpaceProject::checkVictory(void){

	// player 1
	if ( Player1->mPoints > 5000 ){
		sheet_victory->show();					// cegui sheet

		itoa( Player1->mPoints, buffer, 10);
		CEGUI::String strMessage(" Player 1 WINS !! \n Points:");
		strMessage.append( buffer );
		strMessage.append(" \n Time:");
		itoa( mCounterTime, buffer, 10);
		strMessage.append( buffer );

		sheet_victory->setText(strMessage);
		mGAMEON = false;
		// parar sonidos
		mSoundMgr->stopAllAudio();

		// reset
		Player1->mPoints = 0;
	}
	// player 2
	if ( Player2->mPoints > 5000 ){
		sheet_victory->show();					// cegui sheet

		itoa( Player2->mPoints, buffer, 10);
		CEGUI::String strMessage(" Player 2 WINS !! \n Points:");
		strMessage.append( buffer );
		strMessage.append(" \n Time:");
		itoa( mCounterTime, buffer, 10);
		strMessage.append( buffer );

		sheet_victory->setText(strMessage);
		mGAMEON = false;
		// parar sonidos
		mSoundMgr->stopAllAudio();

		// reset
		Player2->mPoints = 0;
	}

}

// -----------------------------------------------------------------------------
// Count laps
void SpaceProject::countLaps(void){

	// Player 1
	if ( Player1->inGoal ){
		// put one lap more
			// Changed Time Counter
			char buffer[33];
			Player1->mPoints += 2000;					// incremento contador P1
			itoa( Player1->mPoints, buffer, 10);
			CEGUI::String strPointsP1(buffer);
			cont_points1->setText(strPointsP1);

		// set in goal false
		Player1->inGoal = false;

		//sound
		mSoundMgr->playAudio(checkinSound, false);
	}

	// Player 2
	if ( Player2->inGoal ){
		// put one lap more
			// Changed Time Counter
			char buffer[33];
			Player2->mPoints += 2000;					// incremento contador P1
			itoa( Player2->mPoints, buffer, 10);
			CEGUI::String strPointsP2(buffer);
			cont_points2->setText(strPointsP2);

		// set in goal false
		Player2->inGoal = false;

		//sound
		mSoundMgr->playAudio(checkinSound, false);
	}

}

// -----------------------------------------------------------------------------
// Count Speed
void SpaceProject::countSpeed(void){

	// Speed P1
	itoa(Player1->shipSpeed, buffer, 10);
	CEGUI::String strSpeedCounterP1(buffer);
	cont_speed1->setText(strSpeedCounterP1);

	// Speed P2
	itoa(Player2->shipSpeed, buffer, 10);
	CEGUI::String strSpeedCounterP2(buffer);
	cont_speed2->setText(strSpeedCounterP2);
}

// -----------------------------------------------------------------------------
// Hit enemies
void SpaceProject::hitEnemies(void){

	// --------- Pruebas
	Player1->shipNode->translate( 0.0 , 0.0 , 1.0 , Ogre::Node::TS_LOCAL );
	Ogre::Vector3 shipAvanzada = Player1->shipNode->getPosition();
	Player1->shipNode->translate( 0.0 , 0.0 , -1.0 , Ogre::Node::TS_LOCAL );
	
	Ogre::Entity *tmpEnt = NULL;

	// Player 1
	if ( Player1->control_misil ){
		// set the ray origin 0.0 for the collision
		if (mCollisionTools->collidesWithEntityCustom(Player1->shipNode->getPosition(), shipAvanzada, tmpEnt, 100.0f , -5.0f, ENEMY_MASK))
			{
				Ogre::String nameEnt = tmpEnt->getParentSceneNode()->getName();				
				// Is a enemy
				if (nameEnt[0] == 'E'){
					for(int i=0; i<nEnemies; i++){
						if( (nameEnt == Enemies[i]->enemyNode->getName()) &&(Enemies[i]->isAlive) ){
							Enemies[i]->damage(35);
							// Changed POINTS Counter
							char buffer[33];
							Player1->mPoints += 100;					// incremento contador P1
							itoa(Player1->mPoints, buffer, 10);
							CEGUI::String strPointsP1(buffer);
							cont_points1->setText(strPointsP1);
							mSoundMgr->playAudio(zombieSound, false);
						}
					}					
				}
			}
	}

	// --------- Pruebas
	Player2->shipNode->translate( 0.0 , 0.0 , 1.0 , Ogre::Node::TS_LOCAL );
	shipAvanzada = Player2->shipNode->getPosition();
	Player2->shipNode->translate( 0.0 , 0.0 , -1.0 , Ogre::Node::TS_LOCAL );
	// Player 2
	if ( Player2->control_misil ){
		// set the ray origin 0.0 for the collision
		if (mCollisionTools->collidesWithEntityCustom(Player2->shipNode->getPosition(), shipAvanzada, tmpEnt, 100.0f , -5.0f, ENEMY_MASK))
			{
				Ogre::String nameEnt = tmpEnt->getParentSceneNode()->getName();				
				// Is a enemy
				if (nameEnt[0] == 'E'){
					for(int i=0; i<nEnemies; i++){
						if( (nameEnt == Enemies[i]->enemyNode->getName()) &&(Enemies[i]->isAlive) ){
							Enemies[i]->damage(35);
							// Changed POINTS Counter
							char buffer[33];
							Player2->mPoints += 100;					// incremento contador P1
							itoa(Player2->mPoints, buffer, 10);
							CEGUI::String strPointsP2(buffer);
							cont_points2->setText(strPointsP2);
							mSoundMgr->playAudio(zombieSound, false);
						}
					}
				}
			}
	}

}

// -----------------------------------------------------------------------------
// Iotracker
bool SpaceProject::processIotrackerInput(const Ogre::FrameEvent& evt)
{
	//targetNode->setPosition( Ogre::Vector3( data.pos[0]*10, data.pos[1]*10, data.pos[2]*10 ) );
	//targetNode->setOrientation( data.quat[3], data.quat[0], data.quat[1], data.quat[2] );
	targetNode->setOrientation( data.quat[3], targetNode->getOrientation().x, data.quat[1]/2, targetNode->getOrientation().z );

	return true;
}
//-------------------------------------------------------------------------------------
void VRPN_CALLBACK SpaceProject::handleIotracker(void* userData, const vrpn_TRACKERCB t)
{
	if( (ControlPlayer1 == IOTRACKER)||(ControlPlayer2 == IOTRACKER) ){
			vrpn_TRACKERCB *pData = &(((SpaceProject*)userData)->data);

			if (t.sensor == 3) {
			*pData = t;
			pData->pos[0] /= 1000.0; // mm to m
			pData->pos[1] /= 1000.0;
			pData->pos[2] /= 1000.0;
			}
	}
}
//-------------------------------------------------------------------------------------
void VRPN_CALLBACK SpaceProject::handle_iotracker_Button(void* userData, const vrpn_BUTTONCB b)
{
	if(ControlPlayer1 == IOTRACKER){
		// check button W for increase or decrease speed
		if(b.button == 81){
			if(b.state == 1) {
				((SpaceProject*)userData)->Player1->acelerate = true;
				((SpaceProject*)userData)->Player1->decelerate = false;
			}
		}
		// check S for breaks
		if(b.button == 73 && b.state == 1) {
			if(b.state == 1) {
				((SpaceProject*)userData)->Player1->decelerate = true;
				((SpaceProject*)userData)->Player1->acelerate = false;
			}
		}
	}

	if(ControlPlayer2 == IOTRACKER){
		// check button W for increase or decrease speed
		if(b.button == 81){
			if(b.state == 1) {
				((SpaceProject*)userData)->Player2->acelerate = true;
				((SpaceProject*)userData)->Player2->decelerate = false;
			}
		}
		// check S for breaks
		if(b.button == 73 && b.state == 1) {
			if(b.state == 1) {
				((SpaceProject*)userData)->Player2->decelerate = true;
				((SpaceProject*)userData)->Player2->acelerate = false;
			}
		}
	}
}

// ---------------------------------------------------------------------------------------------
// Joystick
void VRPN_CALLBACK SpaceProject::handle_joystick_analog( void* userData, const vrpn_ANALOGCB a )
{
	// for player 1
	if (ControlPlayer1 == JOYSTICK){
		// Rotation
		((SpaceProject*)userData)->Player1->shipRotY=  -a.channel[0];

		// Speed
		if (a.channel[1] >0){
			((SpaceProject*)userData)->Player1->acelerate = true;
			((SpaceProject*)userData)->Player1->decelerate = false;
		}else{
			((SpaceProject*)userData)->Player1->acelerate = false;
			((SpaceProject*)userData)->Player1->decelerate = true;
		}
	}

	// for player 2
	if (ControlPlayer2 == JOYSTICK){
		// Rotation
		((SpaceProject*)userData)->Player2->shipRotY=  -a.channel[0];

		// Speed
		if (a.channel[1] >0){
			((SpaceProject*)userData)->Player2->acelerate = true;
			((SpaceProject*)userData)->Player2->decelerate = false;
		}else{
			((SpaceProject*)userData)->Player2->acelerate = false;
			((SpaceProject*)userData)->Player2->decelerate = true;
		}
	}

}
// ---------------------------------------------------------------------------------------------
void VRPN_CALLBACK SpaceProject::handle_joystick_Button(void* userData, const vrpn_BUTTONCB b)
{
	// for player 1
	if (ControlPlayer1 == JOYSTICK){
		// check 1 or 2 to change cameras
		if(b.button == 1) ((SpaceProject*)userData)->Player1->changeCamera(1);
		if(b.button == 2) ((SpaceProject*)userData)->Player1->changeCamera(2);
		
		// disparo canon
		if(b.button == 0){
			if (b.state == 1)	((SpaceProject*)userData)->Player1->fireIons(true);
			else ((SpaceProject*)userData)->Player1->fireIons(false);
		}
		// disparo llamans
		if(b.button == 3){
			if (b.state == 1)	((SpaceProject*)userData)->Player1->fireFlames(true);
			else ((SpaceProject*)userData)->Player1->fireFlames(false);
		}
	}

	// for player 2
	if (ControlPlayer2 == JOYSTICK){
		// check 1 or 2 to change cameras
		if(b.button == 1) ((SpaceProject*)userData)->Player2->changeCamera(1);
		if(b.button == 2) ((SpaceProject*)userData)->Player2->changeCamera(2);
		
		// disparo canon
		if(b.button == 0){
			if (b.state == 1)	((SpaceProject*)userData)->Player2->fireIons(true);
			else ((SpaceProject*)userData)->Player2->fireIons(false);
		}
		// disparo llamans
		if(b.button == 3){
			if (b.state == 1)	((SpaceProject*)userData)->Player2->fireFlames(true);
			else ((SpaceProject*)userData)->Player2->fireFlames(false);
		}
	}
}

// ---------------------------------------------------------------------------------------------
// gamepad
void VRPN_CALLBACK SpaceProject::handle_gamepad_analog( void* userData, const vrpn_ANALOGCB a )
{
	// for player 1
	if (ControlPlayer1 == GAMEPAD){
		// Rotation
		((SpaceProject*)userData)->Player1->shipRotY=  -a.channel[3];

		// Speed
		if (a.channel[1] <0){
			((SpaceProject*)userData)->Player1->acelerate = true;
			((SpaceProject*)userData)->Player1->decelerate = false;
		}else{
			((SpaceProject*)userData)->Player1->acelerate = false;
			((SpaceProject*)userData)->Player1->decelerate = true;
		}
	}
	
	// for player 2
	if (ControlPlayer2 == GAMEPAD){
		// Rotation
		((SpaceProject*)userData)->Player2->shipRotY=  -a.channel[3];

		// Speed
		if (a.channel[1] <0){
			((SpaceProject*)userData)->Player2->acelerate = true;
			((SpaceProject*)userData)->Player2->decelerate = false;
		}else{
			((SpaceProject*)userData)->Player2->acelerate = false;
			((SpaceProject*)userData)->Player2->decelerate = true;
		}
	}

}
// ---------------------------------------------------------------------------------------------
void VRPN_CALLBACK SpaceProject::handle_gamepad_Button(void* userData, const vrpn_BUTTONCB b)
{		
	// for player 1
	if (ControlPlayer1 == GAMEPAD){
		// check 1 or 2 to change cameras
		if(b.button == 0) ((SpaceProject*)userData)->Player1->changeCamera(1);
		if(b.button == 1) ((SpaceProject*)userData)->Player1->changeCamera(2);
				
		// disparo canon
		if(b.button == 6){
			if (b.state == 1) ((SpaceProject*)userData)->Player1->fireIons(true);
			else ((SpaceProject*)userData)->Player1->fireIons(false);
		}
		// disparo llamans
		if(b.button == 7){
			if (b.state == 1) ((SpaceProject*)userData)->Player1->fireFlames(true);
			else ((SpaceProject*)userData)->Player1->fireFlames(false);
		}
	}

	// for player 2
	if (ControlPlayer2 == GAMEPAD){
		// check 1 or 2 to change cameras
		if(b.button == 0) ((SpaceProject*)userData)->Player2->changeCamera(1);
		if(b.button == 1) ((SpaceProject*)userData)->Player2->changeCamera(2);
				
		// disparo canon
		if(b.button == 6){
			if (b.state == 1) ((SpaceProject*)userData)->Player2->fireIons(true);
			else ((SpaceProject*)userData)->Player2->fireIons(false);
		}
		// disparo llamans
		if(b.button == 7){
			if (b.state == 1) ((SpaceProject*)userData)->Player2->fireFlames(true);
			else ((SpaceProject*)userData)->Player2->fireFlames(false);
		}
	}
}

// ---------------------------------------------------------------------------------------------
// Keyboard
bool SpaceProject::keyPressed( const OIS::KeyEvent &arg )
{
	bool ret = menuCEGUI::keyPressed(arg);

	// player 1
	switch (arg.key)
	{
	case OIS::KC_ESCAPE:
		// parar sonidos
		mSoundMgr->stopAllAudio();
		break;
	case OIS::KC_W:
		Player1->acelerate = true;
	    break;
	case OIS::KC_S:
		Player1->decelerate = true;
	    break;	 
	case OIS::KC_A:
		Player1->shipRotY = Player1->speedRot;// + (Player1->shipSpeed / Player1->maxSpeed);
	    break;	 
	case OIS::KC_D:
		Player1->shipRotY = - ( Player1->speedRot );//+ (Player1->shipSpeed / Player1->maxSpeed) );
	    break;
	case OIS::KC_E:
	    Player1->shipDirection.y = -2;
	    break;	 
	case OIS::KC_Q:
	    Player1->shipDirection.y = 2;
	    break;
	case OIS::KC_1:
		Player1->changeCamera(1);
		break; 
	case OIS::KC_2:
		Player1->changeCamera(2);
		break;
	// Animations
	case OIS::KC_B:
		// disparar bola fuego
		Player1->fireFlames(true);
		break;
	case OIS::KC_SPACE:
		// disparar iones
		Player1->fireIons(true);
		break;		
	case OIS::KC_J:
		// animacion ajuste alas
		Player1->control_ajuste = true;
		break;
	case OIS::KC_K:
		// animacion similacion aterrizar
		Player1->control_aterrizar = true;
		//if (indexTipoNave == 0) anim_aterrizar->setEnabled(true);
		break;
	default:
	    break;
	}

	// for player 2
	if (ControlPlayer2 == KEYBOARD){
		// player 2
		switch (arg.key)
		{
		case OIS::KC_NUMPAD8:
			Player2->acelerate = true;
			break;
		case OIS::KC_NUMPAD5:
			Player2->decelerate = true;
			break;	 
		case OIS::KC_NUMPAD4:
			Player2->shipRotY = Player2->speedRot;// + (Player2->shipSpeed / Player2->maxSpeed);
			break;	 
		case OIS::KC_NUMPAD6:
			Player2->shipRotY = - ( Player2->speedRot);// + (Player2->shipSpeed / Player2->maxSpeed) );
			break;
		case OIS::KC_NUMPAD7:
			Player2->shipDirection.y = -2;
			break;	 
		case OIS::KC_NUMPAD9:
			Player2->shipDirection.y = 2;
			break;
		case OIS::KC_NUMPAD1:
			Player2->changeCamera(1);
			break; 
		case OIS::KC_NUMPAD2:
			Player2->changeCamera(2);
			break;
		// Shoot
		case OIS::KC_NUMPAD0:
			// disparar bola fuego
			Player2->fireFlames(true);
			break;
		case OIS::KC_NUMPADENTER:
			// disparar iones
			Player2->fireIons(true);
			break;
		default:
			break;
		}
	}
	
	return ret;
}
// ---------------------------------------------------------------------------------------------
bool SpaceProject::keyReleased( const OIS::KeyEvent &arg )
{
	bool ret = menuCEGUI::keyReleased(arg);
	
	// player 1
	switch (arg.key)
	{
	case OIS::KC_W:
		Player1->acelerate = false;
	    break;	 
	case OIS::KC_S:
		Player1->decelerate = false;
	    break;	 
	case OIS::KC_A:
		Player1->shipRotY = 0;
	    break;	 
	case OIS::KC_D:
		Player1->shipRotY = 0;
	    break;	 
	case OIS::KC_E:
	    Player1->shipDirection.y = 0;
	    break;	 
	case OIS::KC_Q:
	    Player1->shipDirection.y = 0;
	    break;
	case OIS::KC_B:
		// disparar bola fuego
		Player1->fireFlames(false);
		break;
	case OIS::KC_SPACE:
		// disparar iones
		Player1->fireIons(false);
		break;
	default:
	    break;
	}
	
	// for player 2
	if (ControlPlayer2 == KEYBOARD){
		// player 2
		switch (arg.key)
		{
		case OIS::KC_NUMPAD8:
			Player2->acelerate = false;
			break;
		case OIS::KC_NUMPAD5:
			Player2->decelerate = false;
			break;	 
		case OIS::KC_NUMPAD4:
			Player2->shipRotY = 0;
			break;	 
		case OIS::KC_NUMPAD6:
			Player2->shipRotY = 0;
			break;
		case OIS::KC_NUMPAD7:
			Player2->shipDirection.y = 0;
			break;	 
		case OIS::KC_NUMPAD9:
			Player2->shipDirection.y = 0;
			break;
		// Shoot
		case OIS::KC_NUMPAD0:
			// disparar bola fuego
			Player2->fireFlames(false);
			break;
		case OIS::KC_NUMPADENTER:
			// disparar iones
			Player2->fireIons(false);
			break;
		default:
			break;
		}
	}

	return ret;
}

// ---------------------------------------------------------------------------------------------

#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
#define WIN32_LEAN_AND_MEAN
#include "windows.h"
#endif

#ifdef __cplusplus
extern "C" {
#endif

#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
	INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
#else
	int main(int argc, char *argv[])
#endif
	{
		// Create application object
		SpaceProject app;

		try {
			app.go();
		} catch( Ogre::Exception& e ) {
					#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
						MessageBox( NULL, e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
					#else
						std::cerr << "An exception has occured: " <<
							e.getFullDescription().c_str() << std::endl;
					#endif
		}

		return 0;
    	}

#ifdef __cplusplus
}
#endif